package com.atguigu.day26.day27morning.m1;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class JDBCUtils {
    static DataSource dataSource;

    static {
        //1.创建Properties
        Properties p = new Properties();
        //2.加载配置文件
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            p.load(is);
            //3.通过Properties中的配置信息构建连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 获取连接
     * @return
     */

    public static Connection getConnection() {
        try {
            //4.通过连接池获取连接
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
